package com.shinhye.smartsms;

import java.util.Date;

import com.shinhye.smartsms.utils.CommonUtils;
import com.shinhye.smartsms.utils.MyAsyncQueryHandler;
import com.shinhye.smartsms.utils.SmsUris;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TableRow;
import android.widget.TextView;

public class ConversationDetailActivity extends Activity implements OnClickListener{
	private final int COLUMN_BODY = 1;
	private final int COLUMN_DATE = 2;
	private final int COLUMN_TYPE = 3;
	
	private ListView lv_conv_detail;
	private CursorAdapter adapter;
	private EditText et_sms;
	private String address;
	private MyAsyncQueryHandler asyncQueryHandler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_conv_detail);
		
		Intent intent = getIntent();
		address = intent.getStringExtra("address");
		System.out.println("extras::" + address);
		
		setConvTitle(address);
		
		et_sms = (EditText) findViewById(R.id.et_sms);
		lv_conv_detail = (ListView) findViewById(R.id.lv_conv_detail);
		Button btn_back = (Button) findViewById(R.id.btn_back);
		Button btn_sendsms = (Button) findViewById(R.id.btn_sendsms);
		
		btn_back.setOnClickListener(this);
		btn_sendsms.setOnClickListener(this);
		adapter = new ConvDetailCursorAdapter(this, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		lv_conv_detail.setAdapter(adapter);
		
		asyncQueryHandler = new MyAsyncQueryHandler(getContentResolver());
		asyncQueryHandler.setOnChangeCursorListener(new MyAsyncQueryHandler.OnChangeCursorListener() {
			@Override
			public void preFunc(int token, Object cookie, Cursor cursor) {}
			@Override
			public void afterFunc(int token, Object cookie, Cursor cursor) {
				//第一次进入，收到短信，发送短信，都可以设置最后一条信息显示出来
				lv_conv_detail.setSelection(lv_conv_detail.getCount() - 1);
			}
		});
		
		getData();
	}

	private void getData() {
		String[] projection = new String[]{
				"_id",
				"body",
				"date",
				"type"
		};
		asyncQueryHandler.startQuery(0, adapter, SmsUris.SMS_URI, projection, "address=?", new String[]{address}, "date asc");
	}

	private void setConvTitle(String address) {
		TextView tv_conv_title = (TextView) findViewById(R.id.tv_conv_title);
		String contactName = CommonUtils.getContactName(getContentResolver(), address);
		if(TextUtils.isEmpty(contactName))
			tv_conv_title.setText(address);
		else
			tv_conv_title.setText(contactName);
	}
	
	class ConvDetailCursorAdapter extends CursorAdapter {
		public ConvDetailCursorAdapter(Context context, Cursor c, int flags) {
			super(context, c, flags);
		}

		@Override
		protected void onContentChanged() {
			getData();
		}
		
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = View.inflate(ConversationDetailActivity.this, R.layout.item_conv_detail, null);
			ViewHolder holder = new ViewHolder();
			
			holder.tr_receive = (TableRow) view.findViewById(R.id.tr_receive);
			holder.tv_sms_receive = (TextView) view.findViewById(R.id.tv_sms_receive);
			holder.tv_date_receive = (TextView) view.findViewById(R.id.tv_date_receive);
			holder.tr_send = (TableRow) view.findViewById(R.id.tr_send);
			holder.tv_date_send = (TextView) view.findViewById(R.id.tv_date_send);
			holder.tv_sms_send = (TextView) view.findViewById(R.id.tv_sms_send);
			
			view.setTag(holder);
			return view;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			ViewHolder holder = (ViewHolder) view.getTag();
			
			String body = cursor.getString(COLUMN_BODY);
			long date = cursor.getLong(COLUMN_DATE);
			int type = cursor.getInt(COLUMN_TYPE);
			
			String formattedDate;
			if(DateUtils.isToday(date)) {
				formattedDate = DateFormat.getTimeFormat(context).format(new Date(date));
			} else {
				formattedDate = DateFormat.getDateFormat(context).format(new Date(date));
			}
			
			if(type == 1) {
				holder.tv_date_receive.setText(formattedDate);
				holder.tv_sms_receive.setText(body);
				holder.tr_receive.setVisibility(View.VISIBLE);
				holder.tr_send.setVisibility(View.GONE);
			} else if(type == 2) {
				holder.tv_date_send.setText(formattedDate);
				holder.tv_sms_send.setText(body);
				holder.tr_receive.setVisibility(View.GONE);
				holder.tr_send.setVisibility(View.VISIBLE);
			}
		}
	}
	
	class ViewHolder {
		TableRow tr_receive;
		TextView tv_sms_receive;
		TextView tv_date_receive;
		TableRow tr_send;
		TextView tv_date_send;
		TextView tv_sms_send;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_sendsms:
			String text = et_sms.getText().toString().trim();
			if(!TextUtils.isEmpty(text)) {
				CommonUtils.sendSMS(this, address, text);
			}
			
			et_sms.setText("");
			InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
			imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
			break;
		}
	}
}
